﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class AboutInfoEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "admin")
            {
                Response.Redirect("Search.aspx");
            }
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                SQLHelper.getAboutInfoTitles(out dt);
                ddlTitle.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlTitle.Items.Add(new ListItem(dt.Rows[i]["Title"].ToString(), dt.Rows[i]["Id"].ToString()));
                }


                string title = ddlTitle.SelectedItem.Text;
                DataTable m_dt = new DataTable();
                SQLHelper.getAboutInfoByTitle(title, out m_dt);
                CKEditor1.Text = m_dt.Rows[0]["AboutContent"].ToString();

            }
        }

        protected void ddlTitle_SelectedIndexChanged(object sender, EventArgs e)
        {
            string title = ddlTitle.SelectedItem.Text;
            DataTable dt = new DataTable();
            SQLHelper.getAboutInfoByTitle(title, out dt);
            CKEditor1.Text = dt.Rows[0]["AboutContent"].ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ddlTitle.SelectedItem == null)
            {
                Response.Write("<script language = javascript>alert('请选择欲编辑的标题');</script>");
                return;
            }
            if (CKEditor1.Text == null)
            {
                Response.Write("<script language = javascript>alert('请输入内容');</script>");
                return;
            }
            int iresult = SQLHelper.updateAboutInfoById(Convert.ToInt32(ddlTitle.SelectedItem.Value), CKEditor1.Text);
            if (iresult == 1)
            {
                Session.Remove("NewsId");
                Response.Write("<script>alert('编辑成功!');location.href='AboutInfoEdit.aspx';</script>");
            }
            else
            {
                Session.Remove("NewsId");
                Response.Write("<script>alert('编辑失败!');location.href='AboutInfoEdit.aspx';</script>");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AboutInfoEdit.aspx");
        }
    }
}